app.controller('DriverListCtrl', function ($scope, ajax, $modal, toaster) {

	var inserter = new Req.POST({ resource: $scope.resource });
	var deleter = new Req.DELETE({ resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	$scope.reload = $scope.page.loader('driver/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	$scope.queryMap = {};
	$scope.query = function () {
		if ($scope.queryMap) {
			if ($scope.orglist.selected != undefined) {
				$scope.queryMap.org__id = $scope.orglist.selected.id;
			} else if ($scope.orglist.selected == undefined) {
				delete $scope.queryMap.org__id;
			}
			$scope.reload(1, $scope.queryMap);
		}
	};

	$scope.setblack = function (driver) {
		ajax.post('driver/' + driver.id + '/black', null, function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '已成功将司机加入黑名单。');
				$scope.reload(1);
			} else {
				toaster.pop('error', '错误', result.result);
			}
			$scope.reload(1);
		}, function () { toaster.pop('error', '错误', '加入黑名单失败。'); });
	};

	$scope.offblack = function (driver) {
		ajax.post('driver/' + driver.id + '/blackoff', null, function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '已成功将司机移除黑名单。');
				$scope.reload(1);
			} else {
				toaster.pop('error', '错误', result.result);
			}
			$scope.reload(1);
		}, function () { toaster.pop('error', '错误', '移除黑名单失败。'); });
	};

	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		var modalInstance = $modal.open({
			templateUrl: 'template/system/driver/writeModal.html?v=86885e8f40',
			controller: 'DriverModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			if (param.type == 'update') {
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'driver/update';
				$scope.page.mod(updater, $scope.reload, $scope.queryMap);
			} else if (param.type == 'insert') {
				inserter.fields = param.data;
				inserter.resource = 'driver/insert';
				$scope.page.add(inserter, $scope.reload, $scope.queryMap);
			}
		});
	};

	$scope.upload = function (size, current_data) {
		$scope.current_data = angular.copy(current_data);
		var modalInstance = $modal.open({
			templateUrl: 'template/system/driver/upload.html?v=95d2bf1808',
			controller: 'DriverUploadCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			updater.fields = param.data;
			updater.id = param.data.id;
			updater.resource = 'driver/update';
			ajax.call(updater).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '修改信息成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
				$scope.reload(1);
			}, function () {
				toaster.pop('error', '错误', '修改信息失败。');
			});
		});
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'driver/' + deleter.id + '/delete';
				$scope.page.del(deleter, $scope.reload, $scope.queryMap);
			}
		});
	};

	//获取组织列表
	(function () {
		ajax.call(new Req.GET({
			resource: 'organization/list'
		})).then(function (result) {
			$scope.orglist = result.result;
		}, function () { });
	})();
});
